Java开发 - 老虎机 发表于 2019-01-05 | 分类于 Java开发 | | 阅读次数: 字数统计: 678 | 阅读时长 ≈ 3 老虎机 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234package test.iris; import java.awt.Color; import java.awt.FlowLayout;import java.awt.Font;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Random; import javax.swing.BorderFactory;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.SwingConstants;import javax.swing.border.EmptyBorder; public class UI extends JFrame{ private static UI ui = null; static String[] paths = { "1.png", "2.png", "3.png" }; private MyLabel labelLeft = new MyLabel(paths[0], 0);// 左图 private MyLabel labelCenter = new MyLabel(paths[1], 1);// 中图 private MyLabel labelRight = new MyLabel(paths[2], 2);// 右图 private JButton button = new JButton("开始");// 开始按钮 private JLabel labelMessage = new JLabel("");// 结果标签 private UI() { setTitle("老虎机"); setBounds(200, 200, 500, 500); JPanel panel = new JPanel(); setContentPane(panel); panel.setLayout(new GridLayout(3, 1)); panel.setBorder(new EmptyBorder(50, 50, 50, 50)); // 上中下三块面板 JPanel panelTop = new JPanel(); JPanel panelCenter = new JPanel(); JPanel panelBottom = new JPanel(); panel.add(panelTop); panel.add(panelCenter); panel.add(panelBottom); // 三张图片显示区域 panelTop.setLayout(new FlowLayout()); panelTop.add(labelLeft); panelTop.add(labelCenter); panelTop.add(labelRight); // 开始按钮面板 panelCenter.setLayout(new GridLayout(1, 1)); panelCenter.setBorder(new EmptyBorder(30, 100, 30, 100)); button.addActionListener(new MyListener()); panelCenter.add(button); // 抽奖结果面板 panelBottom.setLayout(new GridLayout(1, 1)); labelMessage.setBorder(BorderFactory.createLineBorder(Color.BLACK)); labelMessage.setFont(new Font("微软雅黑",0 , 40)); labelMessage.setForeground(Color.red); labelMessage.setHorizontalAlignment(SwingConstants.CENTER); panelBottom.add(labelMessage); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public static UI getInstance() { return UI.ui; } public static String[] getPaths() { return paths; } public MyLabel getLabelLeft() { return labelLeft; } public MyLabel getLabelCenter() { return labelCenter; } public MyLabel getLabelRight() { return labelRight; } public JButton getButton() { return button; } public JLabel getLabelMessage() { return labelMessage; } public static void main(String[] args) { UI.ui = new UI(); }} class MyListener implements ActionListener{ private boolean buttonFlag = false;// 按钮点击状态,默认未点击 public static boolean threadFlag = true;//线程中断标志 @Override public void actionPerformed(ActionEvent e) { UI ui = UI.getInstance(); JButton button = ui.getButton(); JLabel labelMessage = ui.getLabelMessage(); MyLabel labelLeft = ui.getLabelLeft(); MyLabel labelCenter = ui.getLabelCenter(); MyLabel labelRight = ui.getLabelRight(); if (buttonFlag) { threadFlag = false; buttonFlag = false; button.setText("开始"); synchronized (labelLeft) //等待三个线程释放锁 { synchronized (labelCenter) { synchronized (labelRight) { int leftId = labelLeft.getId(); int centerId = labelCenter.getId(); int rightId = labelRight.getId(); String result = getResult(leftId, centerId, rightId); labelMessage.setText(result); } } } } else { threadFlag = true; buttonFlag = true; new MyThread(220, labelLeft).start(); new MyThread(150, labelCenter).start(); new MyThread(60, labelRight).start(); labelMessage.setText(""); button.setText("暂停"); } } public static String getResult(int leftId, int centerId, int rightId) { String result = null; if (leftId == centerId & leftId == rightId) result = "一等奖"; else if (leftId == centerId || leftId == rightId || centerId == rightId) result = "二等奖"; else result = "未中奖"; return result; }} class MyLabel extends JLabel{ private int id;// 标签id public MyLabel(String path, int id) { ImageIcon icon = new ImageIcon(path); setIcon(icon); this.id = id; } public int getId() { return id; } public void setId(int id) { this.id = id; } } class MyThread extends Thread{ private long time; private MyLabel label; private Random random = new Random(); public MyThread(long time, MyLabel label) { this.time = time; this.label = label; } @Override public void run() { synchronized (label) //锁住lable对象 { while (MyListener.threadFlag) { try { Thread.sleep(time); int id = random.nextInt(3); label.setId(id); ImageIcon icon = new ImageIcon(UI.paths[id]); label.setIcon(icon); } catch (InterruptedException e) { e.printStackTrace(); } } } }}